<style>
    textarea {
        border-style: none !important;
        resize: none !important;
    }

</style>
<div class="layui-fluid">
    <div class="layui-tab" lay-filter="test1">
        <ul class="layui-tab-title">
            {foreach $apply_type as $vo}
            <li data-id="{$vo.id}" {if $vo.id eq 0} class="layui-this" {/if}>{$vo.name}</li>
            {/foreach}
        </ul>
        <div class="layui-tab-content">
            <div style="padding-top: 20px;">
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                        class="layui-icon layui-icon-refresh-3"></i>
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('applies/add')?$auth->check('applies/add'):'hide'}"
                        title="添加" data-type="add"><i
                        class="layui-icon layui-icon-add-1"></i>添加
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('applies/del')?$auth->check('applies/del'):'hide'}"
                        title="删除" data-type="batchdel"><i
                        class="layui-icon layui-icon-delete"></i>删除
                </button>
            </div>
            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="table-useradmin-admin">
                {{#  if(d.status == '2'  && d.task_status >= 2){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('applies/detail')?$auth->check('applies/detail'):'hide'}"
                   lay-event="detail"><i class="iconfont icon-shenhe6"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('Common/flowchart')?$auth->check('Common/flowchart'):'hide'}"
                   lay-event="flowchart"><i class="iconfont icon-shenhe6"></i>查看流程图</a>
                {{#  } else if(d.status == '1' && d.task_status >= 2) { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('applies/detail')?$auth->check('applies/detail'):'hide'}"
                   lay-event="detail"><i class="iconfont icon-shenhe6"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('Common/flowchart')?$auth->check('Common/flowchart'):'hide'}"
                   lay-event="flowchart"><i class="iconfont icon-shenhe6"></i>查看流程图</a>
                {{#  } else if(d.edit_auth == '1' && d.status != -1) { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('applies/edit')?$auth->check('applies/edit'):'hide'}"
                   lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs {$auth->check('applies/del')?$auth->check('applies/del'):'hide'}"
                   lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
                {{#  } else if(d.edit_auth == '1' && d.status == -1) { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('applies/editsbumit')?$auth->check('applies/editsbumit'):'hide'}"
                   lay-event="editsbumit"><i
                        class="layui-icon layui-icon-edit"></i>编辑并提交</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs {$auth->check('applies/del')?$auth->check('applies/del'):'hide'}"
                   lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
                {{#  } else { }}
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('applies/detail')?$auth->check('applies/detail'):'hide'}"
                   lay-event="detail"><i class="iconfont icon-shenhe6"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('Common/flowchart')?$auth->check('Common/flowchart'):'hide'}"
                   lay-event="flowchart"><i class="iconfont icon-shenhe6"></i>查看流程图</a>
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var listUrl = "{:url('administration/apply')}", create = "{:url('applies/add')}", edit = "{:url('applies/edit')}",
        deleteUrl = "{:url('applies/del')}", signature = "{:url('applies/signature')}",
        detail = "{:url('applies/detail')}", flowchart = "{:url('Common/flowchart')}",
        record = "{:url('applies/record')}",editsubmit = "{:url('applies/editsbumit')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'element'], function () {
        var $ = layui.jquery, table = layui.table
            , element = layui.element, form = layui.form;

        var id = 0; //初始化默认全部
        var type_id = 0;
        element.on('tab(test1)', function (data) {
            var id = $('.layui-this').data('id');
            type_id = $('.layui-this').data('id');
            initialize(id); //初始化
        });
        initialize(id); //初始化
        function initialize(id) {
            var param = $.param({'id': id});
            table.render({
                elem: "#LAY-user-back-manage", url: listUrl + '?' + param
                , cellMinWidth: 100
                ,size:"sm"
                , cols: [
                    [
                        {type: "checkbox", fixed: "left"},
                        {field: "id", align: "center", title: "id"},
                        {field: "username", align: "center", title: "申请人"},
                        {field: "group_name", align: "center", title: "部门"},
                        {
                            field: "appliyType", align: "center", title: "类别", templet: function (e) {
                                if (e.appliyType != null) {
                                    return e.appliyType.name;
                                } else {
                                    return "未知";
                                }
                            }
                        },
                        {
                            field: "money", align: "center", title: "金额",templet:function (e) {
                                if (e.money !=null){
                                    return e.money.jk_money;
                                }else{
                                    return  "不属于借款类别"
                                }
                            }
                        },
                        {
                            field: "status_type", align: "center", title: "状态"
                        },
                        {title: "操作", align: "center", fixed: "right", toolbar: "#table-useradmin-admin"}]],
                response: {
                    statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
                },
                parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code,
                        "count": res.total, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                },
                text: {
                    none: '呀！一条数据都没有'
                },
                page: true,
                limit: 15,
            });
        }

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });
        //按钮触发
        table.on("tool(LAY-user-back-manage)", function (e) {
            var param = $.param({'id': e.data.id});
            if ("edit" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑'
                    , content: edit + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: edit + '?' + param,
                                type: 'post',
                                data: field,
                                // beforeSend: function () {
                                //     layer.load(2, {
                                //         shade: [0.6, '#fff'], content: '数据更新中...', success: function (layero) {
                                //             layero.find('.layui-layer-content').css({
                                //                 'padding-top': '6px',
                                //                 'width': '150px',
                                //                 'padding-left': '40px'
                                //             });
                                //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                //                 'width': '150px !important',
                                //                 'background-position': '2px 0 !important'
                                //             });
                                //         }
                                //     });
                                // },
                                success: function (res) {
                                    console.log(res);
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.closeAll(); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }else if ("editsbumit" === e.event){
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑并提交'
                    , content: editsubmit + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: editsubmit + '?' + param,
                                type: 'post',
                                data: field,
                                // beforeSend: function () {
                                //     layer.load(2, {
                                //         shade: [0.6, '#fff'], content: '数据更新中...', success: function (layero) {
                                //             layero.find('.layui-layer-content').css({
                                //                 'padding-top': '6px',
                                //                 'width': '150px',
                                //                 'padding-left': '40px'
                                //             });
                                //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                //                 'width': '150px !important',
                                //                 'background-position': '2px 0 !important'
                                //             });
                                //         }
                                //     });
                                // },
                                success: function (res) {
                                    console.log(res);
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.closeAll(); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            } else if ("del" === e.event) {
                var params = $.param({'ids': e.data.id});
                layer.confirm("确定删除此信息吗？", function (t) {
                    $.ajax({
                        url: deleteUrl + '?' + params, success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.closeAll(); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                })
            } else if ("flow" === e.event) {
                layer.confirm("确定审批此信息吗？", function (t) {
                    layer.prompt({
                        formType: 2
                        // skin: 'layui-layer-molv'
                        , value: ' ', //给个空字符串 默认可以提交为空！
                        title: '请输入批注信息',
                        area: ['100%', '100%'],
                        btn: ['通过', '不通过', '取消'],    // 自定义设置多个按钮
                        btn2: function (index, elem) {
                            // 得到value
                            var value = layui.jquery('#layui-layer' + index + " .layui-layer-input").val();
                            if (!value) {
                                return false;// 如果value为空，停止继续执行
                            }
                            var param = $.param({'id': e.data.id, 'content': value, 'type': '不通过'});
                            layer.open({
                                type: 2
                                , skin: 'layui-layer-molv'
                                , title: '电子签名'
                                , content: signature + '?' + param
                                , area: ['30%', '50%']
                                , maxmin: true
                            });
                            layer.close(index);
                        },
                        btnAlign: 'c',    // 设置按钮位置
                    }, function (value, index, elem) {
                        var param = $.param({'id': e.data.id, 'content': value, 'type': '通过'});
                        layer.open({
                            type: 2
                            , skin: 'layui-layer-molv'
                            , title: '电子签名'
                            , content: signature + '?' + param,
                                area: ['100%', '100%']
                            , maxmin: true
                        });
                        layer.close(index);
                    });
                    layer.close(t);
                });
            } else if ("liucheng" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '电子签名'
                    , content: record + '?' + param
                    , area: ['30%', '50%']
                    , maxmin: true
                });
            } else if ("detail" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '查看'
                    , content: detail + '?' + param
                    , area: ['80%', '80%']
                    , maxmin: true
                });
            } else if ("flowchart" == e.event) {
                var params = $.param({'id': e.data.id, 'table': 'applies'});
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '查看流程图'
                    , content: flowchart + '?' + params
                    ,    area: ['100%', '100%']
                    , maxmin: true
                });
            }
        });
        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var ids = $.param({'ids': id});
                layer.confirm('确定删除吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: deleteUrl,
                        type: "post",
                        data: ids,
                        beforeSend: function () {
                            layer.load(2, {
                                shade: [0.6, '#fff'], content: '数据删除中...', success: function (layero) {
                                    layero.find('.layui-layer-content').css({
                                        'padding-top': '6px',
                                        'width': '150px',
                                        'padding-left': '40px'
                                    });
                                    layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                        'width': '150px !important',
                                        'background-position': '2px 0 !important'
                                    });
                                }
                            });
                        },
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.closeAll(); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                    // table.reload('LAY-user-back-manage');
                    // layer.msg('已删除');
                });
            }
            , add: function () {
                var param = $.param({'type_id': type_id});
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '添加'
                    , content: create + '?' +param
                    , area: ['100%', '100%']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，静态更新表格中的数据
                            if (field.flow_id == "") {
                                layer.msg("请选择对应的工作流");
                                return false;
                            }
                            if (field.type_id == "") {
                                layer.msg("请选择类别");
                                return false;
                            }
                            $.ajax({
                                url: create,
                                type: 'post',
                                data: field,
                                // beforeSend: function () {
                                //     layer.load(2, {
                                //         shade: [0.6, '#fff'], content: '数据提交中...', success: function (layero) {
                                //             layero.find('.layui-layer-content').css({
                                //                 'padding-top': '6px',
                                //                 'width': '150px',
                                //                 'padding-left': '40px'
                                //             });
                                //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                //                 'width': '150px !important',
                                //                 'background-position': '2px 0 !important'
                                //             });
                                //         }
                                //     });
                                // },
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.closeAll(); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }, refresh: function () {
                table.reload('LAY-user-back-manage');
            }
        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>